package com.ptmall.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpFragment;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.bean.OrderBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.OrderDetilActivity;
import com.ptmall.app.ui.activity.OrdersActivity;
import com.ptmall.app.ui.activity.PayActivity;
import com.ptmall.app.ui.activity.ThActivity;
import com.ptmall.app.ui.adapter.OrderListAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.xfly.app.ui.CkwlActivity;
import com.xfly.app.ui.WlActivity;
import com.xfly.app.ui.XfOrderDetilActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderFragment extends BaseMvpFragment {

    View view;

    PullableListView mListview;
    public PullToRefreshLayout mRefreshView;
    public static final String ARG_TAB = "tab";

    int mNowTab;
    int page = 1;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    List<OrderBean> mDataLists = new ArrayList<>();
    OrderListAdapter mOrderListAdapter;

    Intent intent;

    public static OrderFragment newInstance(int tab) {
        OrderFragment fragment = new OrderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_TAB, tab);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNowTab = getArguments().getInt(ARG_TAB);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;

    }

    @Override
    public void initViews() {

        mRefreshView = (PullToRefreshLayout) view.findViewById(R.id.refresh_view);
        mListview = (PullableListView) view.findViewById(R.id.listview);

        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;

                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getData();
            }
        });


    }

    @Override
    public void onResume() {
        super.onResume();
        mRefreshView.autoRefresh();
    }

    @Override
    public void initEvents() {
        mOrderListAdapter = new OrderListAdapter(getContext(), null);
        mListview.setAdapter(mOrderListAdapter);
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        mOrderListAdapter.setClickInterface(new OrderListAdapter.ClickInterface() {
            @Override
            public void btn1Click(String status, OrderBean bean) {
                switch (status){
                    case "fh":
                        Intent intent = new Intent(getContext(), WlActivity.class);
                        intent.putExtra("id",bean.id);
                        startActivity(intent);
                        break;
                    case "ckwl":
                        Intent intent1 = new Intent(getContext(),CkwlActivity.class);
                        intent1.putExtra("id",bean.id);
                        intent1.putExtra("list", (Serializable) bean.goods);
                        startActivity(intent1);
                        break;
                }
            }

            @Override
            public void btn2Click( OrderBean bean) {
                Intent intent = new Intent(getContext(), XfOrderDetilActivity.class);
                intent.putExtra("id", bean.id);
                startActivity(intent);
            }
        });
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("status", mNowTab + "");
        map.put("seller", "1");
        map.put("mold", "1");

        apiDataRepository.getOrderList(map, new ApiNetResponse<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> datas) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != datas && datas.size() > 0) {
                        mDataLists.addAll(datas);
                    } else {
                    }
                } else {
                    if (null != datas && datas.size() > 0) {
                        mDataLists.addAll(datas);
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mOrderListAdapter.pushData(mDataLists);
            }

            @Override
            public void onFaile(String info, List<OrderBean> data, Throwable throwable) {
                super.onFaile(info, data, throwable);
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
            }
        });
    }

    private void orderCancel(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.orderCancel(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("取消成功");
                mRefreshView.autoRefresh();
            }
        });
    }

    private void orderDel(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.orderDel(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("删除成功");
                mRefreshView.autoRefresh();
            }
        });
    }

    private void qxtk(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.qxtk(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("取消成功");
                mRefreshView.autoRefresh();
            }
        });
    }

    private void conSh(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.conSh(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("确认收货成功");
                ((OrdersActivity) getActivity()).mTradeViewpager.setCurrentItem(4);
//                mRefreshView.autoRefresh();
            }
        });
    }

}
